package com.itboye.jigongbao.activity

import `in`.srain.cube.views.ptr.PtrFrameLayout
import `in`.srain.cube.views.ptr.PtrHandler2
import android.app.AlertDialog
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AbsListView
import android.widget.AdapterView
import android.widget.ImageView
import android.widget.ListView
import com.itboye.jigongbao.R
import com.itboye.jigongbao.adpapter.HeZhangListAdapter
import com.itboye.jigongbao.app.MyApplication
import com.itboye.jigongbao.base.BaseActivity
import com.itboye.jigongbao.bean.JiYiBiBean
import com.itboye.jigongbao.enums.DetailEnums
import com.itboye.jigongbao.retrofit.base.ResultEntity
import com.itboye.jigongbao.retrofit.presenter.UserPrestener
import com.itboye.jigongbao.utils.Const
import com.itboye.jigongbao.utils.Const.Companion.pageSize
import com.itboye.jigongbao.utils.SPUtils
import com.itboye.jigongbao.utils.TitleBarUtils
import com.itboye.jigongbao.utils.ToastUtil.ByAlert
import com.itboye.jigongbao.widget.BasePtr
import kotlinx.android.synthetic.main.activity_he_zhang.*
import java.io.Serializable
import java.util.*
import kotlin.collections.ArrayList


class HeZhangActivity : BaseActivity(), AdapterView.OnItemClickListener, Observer {
    private var ar: ArrayList<JiYiBiBean.JiYiBiEntity>? = null

    override fun update(p0: Observable?, p1: Any?) {
        var entity: ResultEntity = p1 as ResultEntity
        ptr_hezhang_list_framelayout.refreshComplete()
        if (entity.code != 0) {
            ByAlert.alert(entity.msg)
        } else {
            when (entity.eventType) {
                UserPrestener.By_JgbRecord_queryAuditRecordForWorker_Success -> {
                    var jiyiBiBean = entity.data as JiYiBiBean
                    setListAdapter(jiyiBiBean)
                }
                UserPrestener.By_JgbRecord_queryAuditRecordForWorker_Fail -> {
                    ByAlert.alert(entity.msg)
                }
                UserPrestener.By_JgbRecord_queryAuditRecordForOverseer_Success -> {
                    var jiyiBiBean = entity.data as JiYiBiBean
                    setListAdapter(jiyiBiBean)
                }
                UserPrestener.By_JgbRecord_queryAuditRecordForOverseer_Fail -> {
                    ByAlert.alert(entity.msg)
                }
                else -> {
                }
            }
        }


    }

    private lateinit var adapter: HeZhangListAdapter

    private fun setListAdapter(bean: JiYiBiBean) {
        if (ar == null) {
            ar = ArrayList()
            if (bean.list != null) {
                ar!!.addAll(bean.list)
            }
            adapter = HeZhangListAdapter(HeZhangActivity@ this, ar!!,
                    R.layout.hezhang_list_item_2)
            ac_hezhang_lv!!.setAdapter(adapter)
            ac_hezhang_lv!!.setOnItemClickListener(this)
        } else {
            if (pageIndex == 1) {
                ar!!.clear()
            }
            if (bean.list != null) {
                ar!!.addAll(bean.list)
            }
            adapter.notifyDataSetChanged()
        }

    }

    var ac_hezhang_lv: ListView? = null
    var iv_actionbar_left: ImageView? = null
    var alertDialog: AlertDialog? = null
    var userPresenter: UserPrestener? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_he_zhang
    }

    private var pageIndex: Int = 1

    internal var isAtTop = true
    internal var isAtBottom = false
    override fun initData() {
        TitleBarUtils().initTitlebarStyle1(this, this!!.trans_action_bar_account_hezhang!!, "核账", R.drawable.img_back, "", 0, "")
        BasePtr.setPagedPtrStyle(ptr_hezhang_list_framelayout)
        ptr_hezhang_list_framelayout.setPtrHandler(object : PtrHandler2 {
            override fun onRefreshBegin(frame: PtrFrameLayout?) {
                pageIndex = 1
                query()
            }

            override fun onLoadMoreBegin(frame: PtrFrameLayout?) {
                pageIndex++
                query()
            }

            override fun checkCanDoRefresh(frame: PtrFrameLayout?, content: View?, header: View?): Boolean {
                return isAtTop
            }

            override fun checkCanDoLoadMore(frame: PtrFrameLayout?, content: View?, footer: View?): Boolean {
                return isAtBottom
            }

        })
        userPresenter = UserPrestener(this)
        ac_hezhang_lv!!.setOnScrollListener(object :AbsListView.OnScrollListener{
            override fun onScroll(p0: AbsListView?, firstVisibleItem: Int, visibleItemCount: Int, totalItemCount: Int) {
                if (firstVisibleItem == 0) {
                    val firstVisibleItemView = ac_hezhang_lv!!.getChildAt(0)
                    if (firstVisibleItemView != null && firstVisibleItemView!!.getTop() == 0) {
                        isAtTop = true
                        isAtBottom = false
                        Log.d("ListView", "##### 滚动到顶部 #####")
                    } else {
                        isAtTop = false
                    }
                } else if (firstVisibleItem + visibleItemCount == totalItemCount) {
                    val lastVisibleItemView = ac_hezhang_lv!!.getChildAt(ac_hezhang_lv!!.getChildCount() - 1)
                    if (lastVisibleItemView != null && lastVisibleItemView!!.getBottom() == ac_hezhang_lv!!.getHeight()) {
                        Log.d("ListView", "##### 滚动到底部 ######")
                        isAtTop = false
                        isAtBottom = true
                    } else {
                        isAtBottom = false
                    }
                }
            }

            override fun onScrollStateChanged(p0: AbsListView?, p1: Int) {

            }

        })
    }



    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.iv_actionbar_left -> {
                finish()
            }
            R.id.txt_work_times -> {
                alertDialog!!.dismiss()
                intent = Intent(this, HeZhangDetailActivity::class.java)
                intent.putExtra("type", DetailEnums.HEZHANG_TIMES_FOREMAN)
                intent.putExtra("title", "工时明细")
                startActivity(intent)
            }
            R.id.txt_work_things -> {
                alertDialog!!.dismiss()
                intent = Intent(this, HeZhangDetailActivity::class.java)
                intent.putExtra("title", "工事明细")
                intent.putExtra("type", DetailEnums.HEZHANG_THINGS_FOREMAN)
                startActivity(intent)
            }
            else -> {
            }
        }
    }

    override fun onResume() {
        super.onResume()
        query()
    }

    fun query() {
        var role = SPUtils.getSp(Const.ROLE) as String
        when (role) {
            Const.WORKER -> {
                userPresenter!!.By_JgbRecord_queryAuditRecordForWorker(MyApplication!!.instance!!.loginInfo!!.getUid().toString(), MyApplication.instance!!.loginInfo!!.getMobile(), pageIndex, pageSize, this)
            }
            Const.FORCEMAN -> {
                userPresenter!!.By_JgbRecord_queryAuditRecordForOverseer(MyApplication!!.instance!!.loginInfo!!.getUid().toString(), MyApplication.instance!!.loginInfo!!.getMobile(), pageIndex, pageSize, this)
            }
        }
    }

    internal var role = ""
    override fun onItemClick(p0: AdapterView<*>?, p1: View?, position: Int, p3: Long) {
        role = SPUtils.getSp(Const.ROLE).toString()
        val intent = Intent(this, HeZhangDetailActivity::class.java)
        intent.putExtra("title", "明细")
        intent.putExtra("model", ar!![position] as Serializable)
        if (ar!!.get(position).item_type_key == "00S002003") {
            intent.putExtra("type", if (role == Const.FORCEMAN)
                DetailEnums.YIJIBI_FOREMAN_DIANGONG
            else
                DetailEnums.YIJIBI_WORKER_DIANGONG
            )
        }
        if (ar!!.get(position).item_type_key == "00S002002") {
            intent.putExtra("type", if (role == Const.FORCEMAN)
                DetailEnums.YIJIBI_FOREMAN_BAOGONG
            else
                DetailEnums.YIJIBI_WORKER_BAOGONG
            )
        }
        if (ar!!.get(position).item_type_key == "00S002004") {
            intent.putExtra("type", if (role == Const.FORCEMAN)
                DetailEnums.YIJIBI_FOREMAN_JIEZHI
            else
                DetailEnums.YIJIBI_WORKER_JIEZHI
            )
        }
        if (ar!!.get(position).item_type_key == "00S002005") {
            intent.putExtra("type", if (role == Const.FORCEMAN)
                DetailEnums.YIJIBI_FOREMAN_JIESUAN
            else
                DetailEnums.YIJIBI_WORKER_JIESUAN
            )
        }
        intent.putExtra("shenhe", true)
        startActivity(intent)
//        alertDialog = AlertDialog.Builder(this, R.style.AlertDialog).create()
//        var view = View.inflate(this, R.layout.popup_jigongbills_detail, null)
//        alertDialog!!.setView(view)
//        alertDialog!!.show()
//        var txt_work_times: TextView = view.findViewById(R.id.txt_work_times)
//        var txt_work_things: TextView = view.findViewById(R.id.txt_work_things)
//        txt_work_times.setOnClickListener(this)
//        txt_work_things.setOnClickListener(this)
    }
}

